package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.CmsSubjectProductRelationMapper;
import com.shangcheng.model.domain.CmsSubjectProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CmsSubjectProductRelationApiImpl implements CmsSubjectProductRelationApi {
    @Autowired
    private CmsSubjectProductRelationMapper subjectProductRelationMapper;

    @Override
    public void save(List<CmsSubjectProductRelation> subjectProductRelationList, Long id) {
        if (ObjectUtil.isNotEmpty(subjectProductRelationList)) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                cmsSubjectProductRelation.setProductId(id);
                subjectProductRelationMapper.insert(cmsSubjectProductRelation);
            }
        }
    }

    @Override
    public List<CmsSubjectProductRelation> findByProductId(Integer id) {
        return subjectProductRelationMapper.selectList(new LambdaQueryWrapper<CmsSubjectProductRelation>().eq(CmsSubjectProductRelation::getProductId, id));
    }

    @Override
    public void updateById(Integer id, List<CmsSubjectProductRelation> subjectProductRelationList) {
        subjectProductRelationMapper.delete(new LambdaQueryWrapper<CmsSubjectProductRelation>().eq(CmsSubjectProductRelation::getProductId, id));
        this.save(subjectProductRelationList, Convert.toLong(id));
    }
}
